package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.guesstv.lib.module.GtDataProvider;
import com.stark.guesstv.lib.module.bean.TvActorBean;
import com.stark.guesstv1.lib.model.TvPlayBean;
import com.stark.guesstv1.lib.model.TvPlayDataProvider;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SelLevelAdapter;
import flc.ast.databinding.ActivitySelLevelBinding;
import flc.ast.utils.Content;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.FastClickUtil;
import stark.common.basic.utils.SPUtil;

public class SelLevelActivity extends BaseAc<ActivitySelLevelBinding> {

    public static int kind = 0;//1看图猜剧名
    private List<Integer> listShow = new ArrayList<>();//展示的数据
    private int size = 0;//关卡总数
    private int maxPage = 0;//最大页数
    private int page = 0;//当前页数
    private SelLevelAdapter levelAdapter;
    private int oldLevel;
    private String levelName;

    @Override
    protected int onCreate() {
        return R.layout.activity_sel_level;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        mDataBinding.ivSelLevelBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mDataBinding.tvSelLevelLast.setOnClickListener(this);
        mDataBinding.tvSelLevelNext.setOnClickListener(this);
        mDataBinding.rvLevelList.setLayoutManager(new GridLayoutManager(mContext, 5));
        levelAdapter = new SelLevelAdapter();
        mDataBinding.rvLevelList.setAdapter(levelAdapter);
        levelAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.tvSelLevelLast://上一页
                if (page == 0) return;
                page--;
                setData();
                break;
            case R.id.tvSelLevelNext://下一页
                if (page == (maxPage - 1)) return;
                page++;
                setData();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (levelAdapter.getItem(position) > (oldLevel - 1)) return;
        switch (kind){
            case 0://去到看图猜剧名
                Game1Activity.level = position + (30 * page);
                startActivity(Game1Activity.class);
                break;
            case 1://去到看图猜角色
                Game2Activity.level = position;
                startActivity(Game2Activity.class);
                break;
            case 2://去到看图猜类型
                Game3Activity.level = position;
                startActivity(Game3Activity.class);
                break;
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        getLevelData();
    }

    //获取关卡相关数据
    private void getLevelData() {
        switch (kind) {
            case 0://看图猜剧名
                List<TvActorBean> tvData = GtDataProvider.getTvData();
                List<TvActorBean> list1 = new ArrayList<>();
                for (int i = 0; i < tvData.size(); i++) {
                    if (tvData.get(i).name.length() == 4) {//过滤出四字答案关卡
                        list1.add(tvData.get(i));
                    }
                }
                size = list1.size();
                int num = list1.size() / 30;
                if (list1.size() % 30 > 0) {
                    maxPage = num + 1;
                } else {
                    maxPage = num;
                }
                levelName = Content.GAME_NAME1;
                oldLevel = SPUtil.getInt(mContext, Content.GAME_NAME1, 1);//获取对应难度的关卡进度
                mDataBinding.tvSelLevelNum.setText(getString(R.string.level_text) + " " + oldLevel + "/" + size);
                break;
            case 1://看图猜角色
                List<TvActorBean> tvData2 = GtDataProvider.getActorData();
                size = tvData2.size();
                int num2 = tvData2.size() / 30;
                if (tvData2.size() % 30 > 0) {
                    maxPage = num2 + 1;
                } else {
                    maxPage = num2;
                }
                levelName = Content.GAME_NAME2;
                oldLevel = SPUtil.getInt(mContext, Content.GAME_NAME2, 1);//获取对应难度的关卡进度
                mDataBinding.tvSelLevelNum.setText(getString(R.string.level_text) + " " + oldLevel + "/" + size);
                break;
            case 2://看图猜类型
                List<TvPlayBean> tvType = TvPlayDataProvider.getDataForGuessTvType();
                size = tvType.size();
                maxPage = 1;
                levelName = Content.GAME_NAME3;
                oldLevel = SPUtil.getInt(mContext, Content.GAME_NAME3, 1);//获取对应难度的关卡进度
                mDataBinding.tvSelLevelNum.setText(getString(R.string.level_text) + " " + oldLevel + "/" + size);
                break;
        }
        setData();
    }

    //设置数据 一次30条
    private void setData() {
        listShow.clear();
        int count = 30 * page;//计算分页
        for (int i = 0; i < 30; i++) {
            if (i + count < size) {
                listShow.add(i + count);
            }
        }
        levelAdapter.setList(listShow);
        //根据页数给关卡分页
        levelAdapter.setPage(page);
        //根据关卡名称获取过关数量
        levelAdapter.setLevelName(levelName);
        levelAdapter.notifyDataSetChanged();
        mDataBinding.tvSelLevelPage.setText(getString(R.string.page_text) + " " + (page + 1) + "/" + maxPage);
    }

}